package com.jus.player.activity;

import static com.jus.player.config.AppConfig.INT_0;
import static com.jus.player.config.AppConfig.INT_1;
import static com.jus.player.config.AppConfig.INT_2;

import android.annotation.SuppressLint;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.jus.player.R;
import com.jus.player.fragment.main.HomeFragment;
import com.jus.player.fragment.main.MyFragment;
import com.jus.player.fragment.main.PlayListFragment;
import com.jus.player.util.Tag;

import java.util.ArrayList;
import java.util.List;

import xyz.doikki.videoplayer.player.VideoView;

public class MainActivity
        extends BaseActivity<VideoView> implements NavigationBarView.OnItemSelectedListener {

    private final List<Fragment> mFragments = new ArrayList<>();
    public static int mCurrentIndex;

    @Override
    public boolean enableBack() {
        return false;
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view);
        //bottomNavigationView.setSelectedItemId(R.id.tab_play);
        bottomNavigationView.setOnItemSelectedListener(this);
    }

    @Override
    protected void initData() {
        mCurrentIndex = 0;
        mFragments.add(PlayListFragment.newInstance());
        mFragments.add(HomeFragment.newInstance());
        mFragments.add(MyFragment.newInstance());
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.layout_content, mFragments.get(2));
        transaction.add(R.id.layout_content, mFragments.get(1));
        transaction.add(R.id.layout_content, mFragments.get(mCurrentIndex));
        transaction.hide(mFragments.get(2)).hide(mFragments.get(1)).show(mFragments.get(mCurrentIndex));
        transaction.commitAllowingStateLoss();
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        int itemId = menuItem.getItemId();
        int index = INT_0;
        switch (itemId) {
            case R.id.tab_home:
                break;
            case R.id.tab_play:
                index = INT_1;
                break;
            case R.id.tab_my:
                index = INT_2;
                break;
            default:
        }
        if (mCurrentIndex != index) {
            // 切换tab，（释放正在播放的播放器）
            Fragment fragment = mFragments.get(index);
            Fragment curFragment = mFragments.get(mCurrentIndex);

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (fragment.isAdded()) {
                transaction.hide(curFragment).show(fragment);
            } else {
                transaction.add(R.id.layout_content, fragment).hide(curFragment);
            }
            transaction.commitAllowingStateLoss();
            mCurrentIndex = index;
        }
        return true;
    }

    @Override
    public void onBackPressed() {
        // 如果 videoViewManager 处理了返回键事件（对于 Tag.LIST），则直接返回
        if (getVideoViewManager().onBackPress(Tag.RECOMMEND)) {
            return;
        }
        // 如果 videoViewManager 处理了返回键事件（对于 Tag.SEAMLESS），则调用 super.onBackPressed()
        if (getVideoViewManager().onBackPress(Tag.HOT)) {
            super.onBackPressed();
        }
    }
}